package com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.widget

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.InputType
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.Gravity
import android.widget.Toast
import com.vitabuybuy.tv.R
import kotlinx.android.synthetic.main.mlvb_dialog_input_text.*
import razerdp.util.KeyboardUtils

/**
 * 文本输入框
 */
class TextMsgInputDialog(mContext: Context) : Dialog(mContext, R.style.MlvbInputDialog) {
    interface OnTextSendListener {
        fun onTextSend(msg: String?)
    }

    private var mOnTextSendListener: OnTextSendListener? = null
    fun setOnTextSendListener(onTextSendListener: OnTextSendListener?) {
        mOnTextSendListener = onTextSendListener
    }

    init {
        setContentView(R.layout.mlvb_dialog_input_text)
        mlvb_et_input_message.inputType = InputType.TYPE_CLASS_TEXT
        mlvb_rl_outside_view.setOnClickListener { v -> if (v.id != R.id.mlvb_ll_inputdlg_view) dismiss() }
        mlvb_tv_confrim_btn.setOnClickListener {
            val msg = mlvb_et_input_message.text.toString().trim { it <= ' ' }
            if (!TextUtils.isEmpty(msg)) {
                mOnTextSendListener?.onTextSend(msg)
                mlvb_et_input_message.setText("")
                dismiss()
            } else {
                Toast.makeText(
                    mContext,
                    mContext.getString(R.string.mlvb_input_can_not_be_empty),
                    Toast.LENGTH_LONG
                ).show()
            }
        }
//        mlvb_ll_inputdlg_view.addOnLayoutChangeListener { view, i, i1, i2, i3, i4, i5, i6, i7 ->
//            val r = Rect()
//            //获取当前界面可视部分
//            window!!.decorView.getWindowVisibleDisplayFrame(r)
//            //获取屏幕的高度
//            val screenHeight = window!!.decorView.rootView.height
//            //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
//            val heightDifference = screenHeight - r.bottom
//        }
    }

    override fun show() {
        super.show()
        val window = window
        window!!.setGravity(Gravity.BOTTOM)
        window.setWindowAnimations(R.style.bottom_int_out_dialog_style) // 添加动画
        val dm = DisplayMetrics()
        window.windowManager.defaultDisplay.getMetrics(dm)
        window.setLayout(dm.widthPixels, window.attributes.height)
        window.setBackgroundDrawable(ColorDrawable(0x00000000))
        KeyboardUtils.open(mlvb_et_input_message,300)
    }
}